package com.srt.controller.srt;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.srt.model.SrtModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.net.URLEncoder;
import java.util.List;

@Controller
@Slf4j
public class SrtController {

    @RequestMapping("/")
    public String index() {
        return "catalog_list";
    }

    @RequestMapping("/excel_to_srt_index")
    public String excelToSrtIndex() {
        return "srt";
    }

    /**
     * excel 转srt文件
     */
    @PostMapping("/excel_to_srt")
    @ResponseBody
    public void excelToSrt(@RequestParam(value = "file") MultipartFile file, HttpServletResponse response) throws Exception {
        List<SrtModel> srtModelList = ListUtil.list(false);
        EasyExcel.read(new BufferedInputStream(file.getInputStream()), SrtModel.class, new SrtDemoListener(srtModelList)).sheet().doRead();
        //直接写入文件然后导出
        StringBuffer stringBuffer = new StringBuffer();
        int num = 0;
        for (SrtModel srtModel : srtModelList) {
            stringBuffer.append(num + "");
            stringBuffer.append("\r\n");
            stringBuffer.append(srtModel.getStartTime() + " --> " + srtModel.getEndTime());
            stringBuffer.append("\r\n");
            if (StrUtil.isNotEmpty(srtModel.getChContext())) {
                stringBuffer.append(srtModel.getChContext());
                stringBuffer.append("\r\n");
            }
            if (StrUtil.isNotEmpty(srtModel.getEnContext())) {
                stringBuffer.append(srtModel.getEnContext());
                stringBuffer.append("\r\n");
            }
            stringBuffer.append("\r\n");
            num++;
        }
        String fileName = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8") + ".srt");// 设置文件名
        response.getOutputStream().write(stringBuffer.toString().getBytes());
    }

    public static class SrtDemoListener extends AnalysisEventListener<SrtModel> {
        private List<SrtModel> srtModelList;

        public SrtDemoListener(List<SrtModel> srtModelList) {
            this.srtModelList = srtModelList;
        }

        @Override
        public void invoke(SrtModel srtModel, AnalysisContext analysisContext) {
            log.info("解析到一条记录：{}", JSON.toJSONString(srtModel));
            srtModelList.add(srtModel);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            log.info("解析完毕总数：{}", srtModelList.size());
        }
    }
}
